package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.DialogCatAdapter;

import java.util.List;


public class SelectCatgerDialog extends Dialog implements AdapterView.OnItemClickListener {
    private static final String TAG = "SelectListDialog";
    private Context context;
    private AppCompatActivity activity;
    private List<String> tempList;
    private SelectCatgerDialogCallback callback;
    private GridView gv;
    private DialogCatAdapter adapter;


    public SelectCatgerDialog(@NonNull Context context, SelectCatgerDialogCallback callback) {
        super(context);
        this.callback = callback;
        initView(context);
    }


    public SelectCatgerDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        this.activity = (AppCompatActivity) context;
        View root = LayoutInflater.from(context).inflate(R.layout.dialog_menu_cat_list, null);
        gv=root.findViewById(R.id.dgv);
        adapter=new DialogCatAdapter(context);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(this);
        setContentView(root);
        setCanceledOnTouchOutside(true);
    }




    public void showDialog(List<String> list) {
        tempList = list;
        adapter.setItems(list);
        show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }





    private void showToast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(callback!=null){
            callback.selectCatgerDialogResult(tempList.get(position),getType(tempList.get(position)));
        }
        dismiss();
    }

    private int getType(String name){
        if(name.contains("X")){
           return 0;
        }
        if(name.contains("Y")){
            return 1;
        }
        return 3;
    }

    public interface SelectCatgerDialogCallback {
        void selectCatgerDialogResult(String catName, int cookingType);
    }
}
